package com.socket.client;

import com.socket.server.SocketServer;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author wangg
 * @date 2019/4/8 14:18
 */
public class SocketClient {
    final static String ADDRESS = "127.0.0.1";
    private static ThreadFactory tFactory = r -> {
        Thread thread = new Thread(r);
        thread.setName("BsocketClient_" + thread.getId());
        return thread;
    };
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), tFactory);

    static {
        executor.allowCoreThreadTimeOut(true);
    }

    public static String socketSend(String msg) {
        Socket socket = null;
        OutputStream out = null;
        InputStream in = null;
        try {
            socket = new Socket(ADDRESS, SocketServer.PORT);
            in = socket.getInputStream();
            out = socket.getOutputStream();
            //向服务器端发送数据
            out.write(msg.getBytes(StandardCharsets.UTF_8));
            out.flush();
            int c = 0;
            long start = System.currentTimeMillis();
            while (true) {
                if(System.currentTimeMillis()-start>30*100){
                    break;
                }
                if ((c = in.available()) > 0 && c == in.available()) {
                    break;
                }
                Thread.sleep(10);
                in = socket.getInputStream();
            }
            byte[] bytes = new byte[in.available()];
            ByteArrayOutputStream bout =new ByteArrayOutputStream();
            in.read(bytes);
            bout.write(bytes,0,bytes.length);
            return new String(bout.toByteArray(),StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            final String msg = "我是客户端：" + i;
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("接受服务端数据：" + socketSend(msg));
                }
            });
        }

        System.out.println("接受服务端数据：" + socketSend("我是小A"));

    }
}
